﻿@model IEnumerable<BaseData.Web.ViewModels.Department_ProjectVM>

@{
    ViewBag.Title = "部门管理";
    var idx = 0;
}

<div>
    <ol class="breadcrumb">
        <li><a href="../Projects/Index">项目管理</a></li>
        <li><a href="../Departments/Index">部门管理</a></li>
        <li><a href="../Stations/Index">点位管理</a></li>
    </ol>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <table width="100%">
                    <tr>
                        <td>
                            <h2 class="panel-title">部门管理</h2>
                        </td>
                        <td class="pull-right">
                            <a href="#" data-toggle="modal" data-backdrop="static" data-target="#myModal" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-plus"></span> 新建</a>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="panel-body">
                <table class="table table-striped">
                    <tr>
                        <th>
                            序号
                        </th>

                        <th>
                            部门名称
                        </th>
                        <th>
                            所属项目
                        </th>
                        <th>
                            操作
                        </th>
                    </tr>
                    @foreach (var item in Model)
                    {
                        idx++;
                        <tr>
                            <td>
                                @idx
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.DepartmentName)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.ProjectName)

                            </td>
                            <td>
                                <a title="编辑" href="javascript:Edit('@item.DepartmentID');"><span class=" glyphicon glyphicon-edit"></span></a>
                                &nbsp;&nbsp;
                                <a title="删除" href="javascript:Delete('@item.DepartmentID')"><span class="glyphicon glyphicon-trash"></span></a>
                                @*&nbsp;&nbsp;
                                    <a title="添加部门" href="javascript:Add();"><span class="glyphicon glyphicon-plus-sign"></span></a>*@
                            </td>
                        </tr>
                    }
                </table>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">部门新建</h4>
            </div>
            <div class="modal-body">
                <div class=" form-horizontal">

                    <div class="form-group">
                        <label for="projectname" class="col-sm-2 control-label">所属项目</label>
                        <div class="col-sm-10">

                            <select class="form-control" id="projectname">
                                <option value="">默认选择</option>
                            </select>

                        </div>
                    </div>

                    <div class="form-group">
                        <label for="departname" class="col-sm-2 control-label">部门名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="departname"
                                   placeholder="请输入部门名称">
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <div class="alert alert-danger" style="display:none" id="errmsg">输出数据不合法，请检查重新输入！</div>
                <button id="save" type="button" class="btn btn-default btn-sm">
                    <span class="glyphicon glyphicon-ok"></span> 提交
                </button>
                <button type="button" class="btn btn-default btn-sm" data-dismiss="modal"><span style="cursor:pointer" class="glyphicon glyphicon-remove"></span>关闭</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="myEditModal" tabindex="-1" role="dialog" aria-labelledby="myEditModalLable" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myEditModalLable">部门修改</h4>
            </div>
            <div class="modal-body">
                <div class=" form-horizontal">
                    <div class="form-group">
                        <label for="Eprojectname" class="col-sm-2 control-label">所属项目</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="Eprojectname">
                                <option value="">默认选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="edepartname" class="col-sm-2 control-label">部门名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edepartname"
                                   placeholder="请输入部门名称">
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <div class="alert alert-danger" style="display:none" id="ederrmsg">输出数据不合法，请检查重新输入！</div>
                <button id="esave" type="button" class="btn btn-default btn-sm">
                    <span class="glyphicon glyphicon-ok"></span> 提交
                </button>
                <button type="button" class="btn btn-default btn-sm" data-dismiss="modal"><span style="cursor:pointer" class="glyphicon glyphicon-remove"></span>关闭</button>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script>
        $(function () {

            $.getJSON("/api/ProjectsAPI", function (data) {
                var item = "<option value=-1>=请选择=</option>";
                for (var i = 0; i < data.length; i++) {
                    item += ' <option value="' + data[i].ProjectID + '">' + data[i].ProjectName + '</option>';
                }
                $("#projectname").empty().append(item);
                $("#Eprojectname").empty().append(item);
            });

            $("#departname").bind("change", function () {
                $(this).parents(".form-group").attr("class", "form-group");
            });

            $("#edepartname").bind("change", function () {
                $(this).parents(".form-group").attr("class", "form-group");
            });

            $("#projectname").bind("change", function () {
                $(this).parents(".form-group").attr("class", "form-group");
            });

            $("#Eprojectname").bind("change", function () {
                $(this).parents(".form-group").attr("class", "form-group");
            });

            $("#save").bind("click", function () {
                var model = {};
                model.DepartmentName = $("#departname").val();
                model.ProjectID = $("#projectname").val();

                if (model.ProjectID == "-1") {
                    $("#projectname").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.DepartmentName == "") {
                    $("#departname").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                $.ajax({
                    url: "/Departments/Create",
                    type: 'POST',
                    data: { jsonstr: JSON.stringify(model) },
                    success: function (data) {
                        if (data == "OK") {
                            location.href = "/Departments/Index";
                        }
                    },
                    error: function (error) {
                        if (error.status == 500) {
                            $("#errmsg").css("display", "block");
                        }
                    }
                });
            });
        });
        function Edit(id) {
            $.ajax({
                url: "/Departments/Edit",
                type: 'GET',
                data: { id: id },
                success: function (data) {
                    $("#Eprojectname").val(data.ProjectID);
                    $("#edepartname").val(data.DepartmentName);
                    $('#myEditModal').modal('show');
                }
            });

            $("#esave").bind("click", function () {
                var model = {};
                model.DepartmentID = id;
                model.ProjectID = $("#Eprojectname").val();
                model.DepartmentName = $("#edepartname").val();
                if (model.ProjectID == "-1") {
                    $("#Eprojectname").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.DepartmentName == "") {
                    $("#edepartname").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }

                $.ajax({
                    url: "/Departments/Edit",
                    type: 'POST',
                    data: { dep: JSON.stringify(model) },
                    success: function (data) {
                        location.href = "/Departments/Index";
                    },
                    error: function (error) {
                        if (error.status == 500) {
                            $("#ederrmsg").css("display", "block");
                        }
                    }
                });
            });
        }
        function Delete(obj) {
            if (confirm("提示:以下操作将会删除该部门下所有点位,是否继续?")) {
                $.ajax({
                    url: "/Departments/Delete",
                    type: 'GET',
                    data: { id: obj },
                    success: function (data) {
                        location.href = "/Departments/Index";
                    }
                });
            }
        }
    </script>
}